﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC4_Test.Models;

namespace MVC4_Test.Controllers
{
    public class QuestionVotesController : Controller
    {
        private StackOverflowContext cd = new StackOverflowContext();

        public ActionResult Show(int id = 0)
        {
            return View(cd.Questions.Find(id));
        }

        //
        // GET: /Question/
        public ActionResult Index()
        {
            ViewBag.Counter = cd.Questions.Count();

            return View(cd.Questions.Include("QuestionTag").Include("Answer").ToList());
        }

        //
        // GET: /Question/Details/5

        public ActionResult Details(int id = 0)
        {
            Question question = cd.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        // POST: /Question/Create

        [HttpPost]
        public ActionResult Create(Question question)
        {
            if (ModelState.IsValid)
            {
                cd.Questions.Add(question);
                cd.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(question);
        }

        //
        // GET: /Question/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Question question = cd.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // POST: /Question/Edit/5

        [HttpPost]
        public ActionResult Edit(Question question)
        {
            if (ModelState.IsValid)
            {
                cd.Entry(question).State = EntityState.Modified;
                cd.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(question);
        }

        //
        // GET: /Question/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Question question = cd.Questions.Find(id);
            if (question == null)
            {
                return HttpNotFound();
            }
            return View(question);
        }

        //
        // POST: /Question/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Question question = cd.Questions.Find(id);
            cd.Questions.Remove(question);
            cd.SaveChanges();
            return RedirectToAction("Index");
        }


        // seach functie die kijkt naar tag gebaseerd uit een textfield
        // GET: /Question/Search/id
        public ActionResult Search(string q)
        {

            ViewBag.Query = q;

            var result = cd.Questions.Include("Answer").Include("QuestionTag").Where(p => p.Text.Contains(q) || p.Title.Contains(q));
            return View(result);
        }

        protected override void Dispose(bool disposing)
        {
            cd.Dispose();
            base.Dispose(disposing);
        }
    }
}